package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbHomework;
import com.beetlestech.nfe.main.biz.com.dto.VhomeworklistInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.HomeworkhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.StudenthomeworkDTO;
import com.beetlestech.nfe.main.db.view.VhomeworklistDTO;


public class HbHomeworkImpl extends AbstractDBLogic implements HbHomework, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String ST_HOMEWORK = "学生作业表";

	@Override
	public VhomeworklistInfoDTO[] getHomeworkList(Integer userID, Integer homeworkID,
			String resultType, String sortType, String sendDateSortType) {
		List<VhomeworklistInfoDTO> vhomeworkList = new ArrayList<VhomeworklistInfoDTO>();

		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VhomeworklistDTO.COLUMN_USR_ID, StatementUtil.KEYWORD.EQUAL, userID);
		// 作业ID
		if (homeworkID != null) {
			StatementUtil.andWhere(statementDTO, VhomeworklistDTO.COLUMN_HOMEWORK_ID, StatementUtil.KEYWORD.EQUAL, homeworkID);
		}
		// 完成状况
		if (resultType !=null && !"".equals(resultType) && !"all".equals(resultType)) {
			StatementUtil.andWhere(statementDTO, VhomeworklistDTO.COLUMN_STATE, StatementUtil.KEYWORD.EQUAL, Integer.valueOf(resultType));
		}
		VhomeworklistDTO vhomeworklistDTO = new VhomeworklistDTO();
		vhomeworklistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		// 排序
		String columnName = "";
		if (sortType != null && !"".equals(sortType)) {
			// 按类型排序
			if ("1".equals(sortType)) {
				columnName = columnName.concat(VhomeworklistDTO.COLUMN_HOMEWORK_TYPE);

			// 按发送者排序
			} else if ("2".equals(sortType)) {
				columnName = columnName.concat(VhomeworklistDTO.COLUMN_SENDER_NM);
			}
		}

		// 默认按时间降序排序
		if (sendDateSortType == null || "".equals(sendDateSortType)) {
			sendDateSortType = "DESC";
		}
		if (columnName.length() == 0) {
			columnName = VhomeworklistDTO.COLUMN_SEND_DATE + " " + sendDateSortType + ", " + VhomeworklistDTO.COLUMN_SEND_TIME + " " + sendDateSortType;
		} else {
			columnName = columnName + ", " + VhomeworklistDTO.COLUMN_SEND_DATE + " " + sendDateSortType + ", " + VhomeworklistDTO.COLUMN_SEND_TIME + " " + sendDateSortType;
		}
		vhomeworklistDTO.set_orderByCondition(columnName);

		List<VhomeworklistDTO> VHomeworks = queryDAO.queryForObjectList(VhomeworklistDTO.SQL_SEARCH, vhomeworklistDTO);
		for(VhomeworklistDTO item : VHomeworks){
			vhomeworkList.add(convertToHomeworkInfo(item));
		}

		return vhomeworkList.toArray(new VhomeworklistInfoDTO[vhomeworkList.size()]);
	}

	private VhomeworklistInfoDTO convertToHomeworkInfo(VhomeworklistDTO item){
		VhomeworklistInfoDTO vhomeworkInfo = new VhomeworklistInfoDTO();
		vhomeworkInfo.setusrid((item.getusrid()));
		vhomeworkInfo.sethomeworkid(item.gethomeworkid());
		vhomeworkInfo.sethomeworknm(item.gethomeworknm());
		vhomeworkInfo.sethomeworktype(item.gethomeworktype());
		vhomeworkInfo.setsenddate(item.getsenddate());
		vhomeworkInfo.setsendtime(item.getsendtime());
		vhomeworkInfo.setsenderid(item.getsenderid());
		vhomeworkInfo.setsendernm(item.getsendernm());
		vhomeworkInfo.setstate(item.getstate());
		vhomeworkInfo.setfileid(item.getfileid());
		vhomeworkInfo.setissendback(item.getissendback());
		vhomeworkInfo.setpath(item.getpath());
		return vhomeworkInfo;
	}

	@Override
	public void deleteHomework(String chkID, Integer userID) {
		// 明细记录删除
		HomeworkhistoryDTO deleteHomeworkhistoryDTO = new HomeworkhistoryDTO();
		for (String item : chkID.split(",")) {
			if (!StringUtil.isEmpty(item)) {
				deleteHomeworkhistoryDTO.sethomeworkid(Integer.parseInt(item));
				updateDAO.update(SQLID.HOMEWORKHISTORY_DELETE_HWID, deleteHomeworkhistoryDTO);
			}
		}

		// 主表记录删除
		StudenthomeworkDTO deleteHomeworkDTO = new StudenthomeworkDTO();
		for (String item : chkID.split(",")) {
			if (!StringUtil.isEmpty(item)) {
				deleteHomeworkDTO.sethomeworkid(Integer.parseInt(item));
				deleteHomeworkDTO.setusrid(userID);
				updateDAO.updateOne(StudenthomeworkDTO.DELETE, deleteHomeworkDTO, M10190WS, ST_HOMEWORK);
			}
		}
	}

	@Override
	public void updateHomeworkState(String chkID, Integer userID) {
		StudenthomeworkDTO updateHomeworkDTO = new StudenthomeworkDTO();
		for (String item : chkID.split(",")) {
			if (!StringUtil.isEmpty(item)) {
				updateHomeworkDTO.sethomeworkid(Integer.parseInt(item));
				updateHomeworkDTO.setusrid(userID);
				updateHomeworkDTO.setstate(1);
				updateHomeworkDTO.setlastdate(getCurrentDate());
				updateHomeworkDTO.setlasttime(getCurrentTime());
				updateDAO.update(StudenthomeworkDTO.UPDATE, updateHomeworkDTO);
			}
		}
	}
}
